﻿using System;
using System.Collections.Generic;
using System.Web;

namespace BSPortal.Usercontrol
{
    public partial class ProductDetail : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String productId = Request[PageConst.ProductId];
            if (!string.IsNullOrEmpty(productId))
            {
                MainService mainService = Common.GetClientService();
                var Product = mainService.GetProductById(short.Parse(productId));
                if (Product != null)
                {
                    short productLine = Common.GetProductLine(short.Parse(productId));
                    short line = Common.GetLine();

                    if (productLine != line)
                    {
                        string pages = Request.QueryString["Page"];
                        Response.Redirect(string.Format("/{0}-{1}-{2}.html", pages.ToLower(), productLine, productId));
                    }

                    mainService.SetReadTime(Product.ProductId);
                    lblName.Text = Product.Name;
                    
                    lblCategoryName.Text = mainService.GetCategoryById(Product.CategoryId).Name;
                    lblCategoryName.NavigateUrl = string.Format("category-{0}-{1}.html", Common.GetLine(), Product.CategoryId);
                    
                    lblPublisherName.Text = mainService.GetPublisherById((short) Product.PublisherId).Name;
                    lblPublisherName.NavigateUrl = string.Format("publisher-{0}-{1}.html", Common.GetLine(), Product.PublisherId);

                    productImage.ImageUrl = "/images/" + Product.Image;

                    txtContent.InnerHtml = Product.Content;
                    lblPrice.Text = Product.Price.ToString("#,###");
                    lblReadTime.Text = Product.ReadTime.ToString();
                    lblSoldTime.Text = Product.SoldAmount.ToString();
                    lblTechnicInfo.Text = Product.TechnicInfo;
                    lblDiscountPrice.Text =
                        (Math.Round((decimal)((Product.Price * (1 - Product.Discount)) / 100), 0) * 100).ToString("#,###");
                }
            }
        }

        protected void Unnamed1_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            int productId = int.Parse(Request["ProductId"]);
            int amount=1;
            int.TryParse(txtAmount.Text, out amount);
            if (Session["Cart"] != null)
            {
                var cart = (Dictionary<int, int>) System.Web.HttpContext.Current.Session["Cart"];
                if (cart.ContainsKey(productId))
                    cart[productId] += amount;
                else
                    cart.Add(productId, amount);

                System.Web.HttpContext.Current.Session["Cart"] = cart;
            }
            else
            {
                var cart = new Dictionary<int, int>();
                cart.Add(productId, amount);

                System.Web.HttpContext.Current.Session["Cart"] = cart;
            }

            Response.Redirect("/gio-hang.html");
        }
    }
}